package com.handy.plugin.auth;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.database.DBUtil;
import com.handy.plugin.auth.bean.HAuthAction;
import com.handy.plugin.auth.bean.HAuthDynaRoleInfo;
import com.handy.plugin.auth.bean.HAuthDynaUserInfo;
import com.handy.plugin.auth.bean.HAuthRoleInfo;
import com.handy.plugin.auth.bean.HAuthScriptAction;
import com.handy.plugin.auth.bean.HAuthUserInfo;

/**
 * 权限缓存。
 * @author rocken.zeng@gmail.com
 *
 */
public class TAuthCache {

	private TAuthCache(){}
	private static TAuthCache instance = null;
	public static synchronized TAuthCache getInstance(){
		if (instance == null) {
			instance = new TAuthCache();
			instance.load();
		}
		return instance;
	}
	
	/**
	 * 通过actionclass获取authid(即权限树ID号) <br>
	 * 返回：ArrayList(authid)
	 * @param actionclass
	 * @return 
	 */
	public HashSet<Integer> getAuthidsFromAction(String actionclass){
		return authActions.get(actionclass);
	}
	
	/**
	 * 通过scid获取authid(即权限树ID号) <br>
	 * 返回：ArrayList(authid)
	 * @param scid
	 * @return
	 */
	public HashSet<Integer> getAuthidsFromScriptAction(String scid){
		return authScriptActions.get(scid);
	}
	
	/**
	 * 代表变量authRoleInfos
	 */
	public static final int MAP_ROLE_INFO = 1;
	/**
	 * 代表变量authUserInfos
	 */
	public static final int MAP_USER_INFO = 2;
	
	/**
	 * 通过角色编号或用户编号查找该角色或用户的基本权限ID号以及节点上的所有许可(即添加,删除等)。<br>
	 * 返回：HashMap(authid,ArrayList(authkind))
	 * @param id 角色编号或用户编号
	 * @param map 从哪个map中查找
	 * @return HashMap<authid,ArrayList<authkind>>
	 */
	public HashMap<Integer,HashSet<String>> getAuthids(Integer id,int map){
		HashMap<Integer,HashSet<String>> authids = null;
		switch (map){
		case MAP_ROLE_INFO:
			authids = authRoleInfos.get(id);
			break;
		case MAP_USER_INFO:
			authids = authUserInfos.get(id);
			break;
		}
		if (authids == null){authids=new HashMap<Integer,HashSet<String>>();}
		return authids;
	}
	
	/**
	 * 代表变量authRoleInfos
	 */
	public static final int MAP_DYNA_ROLE_INFO = 1;
	/**
	 * 代表变量authUserInfos
	 */
	public static final int MAP_DYNA_USER_INFO = 2;
	
	/**
	 * 通过角色编号或用户编号查找该角色或用户的所有扩展权限以及其上的所有许可。 <br>
	 * 返回：HashMap<authid,HashMap<pcolvalue,HashSet<authkind>>>
	 * @param id 角色编号或用户编号
	 * @param map 从哪个map中查找
	 * @return 
	 */
	public HashMap<Integer,HashMap<String,HashSet<String>>> getDynaAuthids(Integer id,int map){
		HashMap<Integer,HashMap<String,HashSet<String>>> authids = null;
		switch (map){
		case MAP_DYNA_ROLE_INFO:
			authids = authDynaRoleInfos.get(id);
			break;
		case MAP_DYNA_USER_INFO:
			authids = authDynaUserInfos.get(id);
			break;			
		}
		if (authids == null){authids=new HashMap<Integer,HashMap<String,HashSet<String>>>();}
		return authids;
	}
	
	private void load(){
		log.info("权限缓存载入中...");
		//loadAuthAction();
		//loadAuthScriptAction();
		loadAuthRoleInfos();
		loadAuthUserInfos();
		loadAuthDynaRoleInfos();
		loadAuthDynaUserInfos();
		log.info("权限缓存载入完毕。");
		
	}

	/**
	 * 导入action对应的authid,h_auth_actions.actionclass对应多个h_auth_actions.authid。
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthAction(){
		//清空authActions
		clearAuthActions();
		String condition = "order by actionclass";

		ArrayList _authActions = findAll(TABLE_H_AUTH_ACTIONS, condition, HAuthAction.class);
		for (Iterator iter = _authActions.iterator();iter.hasNext();){
			HAuthAction authAction = (HAuthAction)iter.next();
			if (null==authActions.get(authAction.getActionclass())){
				//如果不存在，加入新的
				authActions.put(authAction.getActionclass(), new HashSet<Integer>());
			}
			authActions.get(authAction.getActionclass()).add(authAction.getAuthid());
		}
		if (log.isDebugEnabled()){
			printAuthActions();
		}
	}
	
	/**
	 * 导入ScriptAction脚本编号对应的authid,h_auth_scriptactions.scid对应多个h_auth_actions.authid。
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthScriptAction(){
		//清空authActions
		clearAuthScriptActions();
		String condition = "order by authid,scid";

		ArrayList _authActions = findAll(TABLE_H_AUTH_SCRIPTACTIONS, condition, HAuthScriptAction.class);
		for (Iterator iter = _authActions.iterator();iter.hasNext();){
			HAuthScriptAction authScriptAction = (HAuthScriptAction)iter.next();
//			log.info("authScriptAction.getScid()="+authScriptAction.getScid());
//			log.info("authScriptAction.getAuthid()="+authScriptAction.getAuthid());
			if (null==authScriptActions.get(authScriptAction.getScid())){
				//如果不存在，加入新的
				authScriptActions.put(authScriptAction.getScid(), new HashSet<Integer>());
			}
			authScriptActions.get(authScriptAction.getScid()).add(authScriptAction.getAuthid());
		}
		if (log.isDebugEnabled()){
			printAuthActions();
		}
	}
	
	/**
	 * //HashMap<authid,HashMap<scriptid,HashSet<authkindid>>>
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthScriptKindAction(){
		clearAuthScriptidKind();
		String condition = "order by authid,scid";

		ArrayList _authActions = findAll(TABLE_H_AUTH_SCRIPTACTIONS, condition, HAuthScriptAction.class);
		for (Iterator iter = _authActions.iterator();iter.hasNext();){
			HAuthScriptAction authScriptAction = (HAuthScriptAction)iter.next();
//			log.info("authScriptAction.getScid()="+authScriptAction.getScid());
//			log.info("authScriptAction.getAuthid()="+authScriptAction.getAuthid());
			if(null==authScriptidKind.get(authScriptAction.getAuthid())){
				authScriptidKind.put(authScriptAction.getAuthid(), new HashMap<String, HashSet<Integer>>());
			}
			HashMap<String, HashSet<Integer>>  scriptidkind = authScriptidKind.get(authScriptAction.getAuthid());
			if (null==scriptidkind.get(authScriptAction.getScid())){
				scriptidkind.put(authScriptAction.getScid(), new HashSet<Integer>());
			}
			HashSet<Integer> kind = scriptidkind.get(authScriptAction.getScid());
			kind.add(authScriptAction.getAuthkindid());
			
		}
		if (log.isDebugEnabled()){
			printAuthScriptKindAction();
		}
	}
	
	/**
	 * //HashMap<authid,HashMap<actionname,HashSet<authkindid>>>
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthActionKindAction(){
		clearAuthActionKind();
		String condition = "order by authid,scid";

		ArrayList _authActions = findAll(TABLE_H_AUTH_ACTIONS, condition, HAuthAction.class);
		for (Iterator iter = _authActions.iterator();iter.hasNext();){
			HAuthAction authAction = (HAuthAction)iter.next();
//			log.info("authScriptAction.getScid()="+authScriptAction.getScid());
//			log.info("authScriptAction.getAuthid()="+authScriptAction.getAuthid());
			if(null==authActionKind.get(authAction.getAuthid())){
				authActionKind.put(authAction.getAuthid(), new HashMap<String, HashSet<Integer>>());
			}
			HashMap<String, HashSet<Integer>>  actionkind = authActionKind.get(authAction.getAuthid());
			if (null==actionkind.get(authAction.getActionclass())){
				actionkind.put(authAction.getActionclass(), new HashSet<Integer>());
			}
			HashSet<Integer> kind = actionkind.get(authAction.getActionclass());
			kind.add(authAction.getAuthkindid());
			
		}
		if (log.isDebugEnabled()){
			printAuthActionKindAction();
		}
	}
	
	/**
	 * 通过角色编号取该角色能允许访问的权限编号，通过权限编号能找到该权限上的所有许可。
	 * 1.角色编号对应权限编号；
	 * 2.权限编号对应权限各类。
	 * h_auth_role_infos.roleid对应多个h_auth_role_infos.authid
	 * h_auth_role_infos.authid对应多个h_auth_role_infos.authkind
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthRoleInfos(){
		clearAuthRoleInfos();
		String condition = "order by roleid,authid";
		ArrayList _authRoleInfos = findAll(TABLE_H_AUTH_ROLE_INFOS, condition, HAuthRoleInfo.class);
		for (Iterator iter = _authRoleInfos.iterator();iter.hasNext();){
			HAuthRoleInfo authRoleInfo = (HAuthRoleInfo)iter.next();
			if (null==authRoleInfos.get(authRoleInfo.getRoleid())){
				//如果不存在，加入新的
				authRoleInfos.put(authRoleInfo.getRoleid(), new HashMap<Integer,HashSet<String>>());
			}
			HashMap<Integer,HashSet<String>> authids = authRoleInfos.get(authRoleInfo.getRoleid());
			if (null==authids.get(authRoleInfo.getAuthid())){
				authids.put(authRoleInfo.getAuthid(), new HashSet<String>());
			}
			authids.get(authRoleInfo.getAuthid()).add(authRoleInfo.getAuthkind());
		}
		if (log.isDebugEnabled()){
			printAuthRoleInfos();
		}
	}
	
	/**
	 * 通过用户编号取该角色能允许访问的权限编号，通过权限编号能找到该权限上的所有许可。
	 * 1.用户编号对应权限编号；
	 * 2.权限编号对应权限各类。
	 * h_auth_user_infos.userid对应多个h_auth_user_infos.authid
	 * h_auth_user_infos.authid对应多个h_auth_user_infos.authkind
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthUserInfos(){
		clearAuthUserInfos();
		String condition = "order by userid,authid";
		ArrayList _authUserInfos = findAll(TABLE_H_AUTH_USER_INFOS, condition, HAuthUserInfo.class);
		//log.info("_authUserInfos.size()="+_authUserInfos.size());
		for (Iterator iter = _authUserInfos.iterator();iter.hasNext();){
			HAuthUserInfo authUserInfo = (HAuthUserInfo)iter.next();
			//log.info("authUserInfo.getAuthid()="+authUserInfo.getAuthid());
			//log.info("authUserInfo.getUserid()="+authUserInfo.getUserid());
			if (null==authUserInfos.get(authUserInfo.getUserid())){
				//如果不存在，加入新的
				authUserInfos.put(authUserInfo.getUserid(), new HashMap<Integer,HashSet<String>>());
			}
			HashMap<Integer,HashSet<String>> authids = authUserInfos.get(authUserInfo.getUserid());
			if (null==authids.get(authUserInfo.getAuthid())){
				authids.put(authUserInfo.getAuthid(), new HashSet<String>());
			}
			authids.get(authUserInfo.getAuthid()).add(authUserInfo.getAuthkind());
		}
		if (log.isDebugEnabled()){
			printAuthUserInfos();
		}
	}
	
	/**
	 * 通过角色编号取该角色能允许访问的扩展权限编号，通过扩展权限编号能找到该扩展权限上的所有许可。<br>
	 * HashMap<roleid, HashMap<authid,HashMap<pcolvalue,HashSet<authkind>>>> <br>
	 * 1.角色编号对应动态权限编号；<br>
	 * 2.动态权限编号对应动态权限各类。<br>
	 * h_auth_dyna_role_infos.roleid对应多个h_auth_dyna_role_infos.authkey_pcolvalue <br>
	 * h_auth_dyna_role_infos.authkey_pcolvalue对应多个h_auth_dyna_role_infos.authkind <br>
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthDynaRoleInfos(){
		clearAuthDynaRoleInfos();
		String condition = "order by roleid,authid";
		ArrayList _authDynaRoleInfos = findAll(TABLE_H_AUTH_DYNA_ROLE_INFOS, condition, HAuthDynaRoleInfo.class);
		for (Iterator iter = _authDynaRoleInfos.iterator();iter.hasNext();){
			HAuthDynaRoleInfo authDynaRoleInfo = (HAuthDynaRoleInfo)iter.next();
//			log.info("authDynaRoleInfo.getAuthid()="+authDynaRoleInfo.getAuthid());
//			log.info("authDynaRoleInfo.getRoleid()="+authDynaRoleInfo.getRoleid());
//			log.info("authDynaRoleInfo.getPcolvalue()="+authDynaRoleInfo.getPcolvalue());
//			log.info("authDynaRoleInfo.getAuthkind()"+authDynaRoleInfo.getAuthkind());
			if (null==authDynaRoleInfos.get(authDynaRoleInfo.getRoleid())){
				authDynaRoleInfos.put(authDynaRoleInfo.getRoleid(), new HashMap<Integer,HashMap<String,HashSet<String>>>());
			}
			HashMap<Integer,HashMap<String,HashSet<String>>> authids = authDynaRoleInfos.get(authDynaRoleInfo.getRoleid()); 
			
			if (null==authids.get(authDynaRoleInfo.getAuthid())){
				authids.put(authDynaRoleInfo.getAuthid(), new HashMap<String,HashSet<String>>());
			}			
			HashMap<String,HashSet<String>> pcolvalues = authids.get(authDynaRoleInfo.getAuthid());
			
			if (null==pcolvalues.get(authDynaRoleInfo.getPcolvalue())){
				pcolvalues.put(authDynaRoleInfo.getPcolvalue(), new HashSet<String>());
			}
			pcolvalues.get(authDynaRoleInfo.getPcolvalue()).add(authDynaRoleInfo.getAuthkind());
		}
		if (log.isDebugEnabled()){
			printAuthDynaRoleInfos();
		}
	}
	
	/**
	 * 通过用户编号取该用户能允许访问的扩展权限编号，通过扩展权限编号能找到该扩展权限上的所有许可。<br>
	 * HashMap<userid, HashMap<authid,HashMap<pcolvalue,HashSet<authkind>>>> <br>
	 * 1.用户编号对应扩展权限编号；<br>
	 * 2.扩展权限编号对应扩展权限各类。<br>
	 * h_auth_dyna_user_infos.roleid对应多个h_auth_dyna_user_infos.authkey_pcolvalue <br>
	 * h_auth_dyna_user_infos.authkey_pcolvalue对应多个h_auth_dyna_user_infos.authkind <br>
	 */
	@SuppressWarnings("unchecked")
	public void loadAuthDynaUserInfos(){
		clearAuthDynaUserInfos();
		String condition = "order by Userid,authid";
		ArrayList _authDynaUserInfos = findAll(TABLE_H_AUTH_DYNA_USER_INFOS, condition, HAuthDynaUserInfo.class);
		for (Iterator iter = _authDynaUserInfos.iterator();iter.hasNext();){
			HAuthDynaUserInfo authDynaUserInfo = (HAuthDynaUserInfo)iter.next();
			
			if (null==authDynaUserInfos.get(authDynaUserInfo.getUserid())){
				authDynaUserInfos.put(authDynaUserInfo.getUserid(), new HashMap<Integer,HashMap<String,HashSet<String>>>());
			}
			HashMap<Integer,HashMap<String,HashSet<String>>> authids = authDynaUserInfos.get(authDynaUserInfo.getUserid()); 
			
			if (null==authids.get(authDynaUserInfo.getAuthid())){
				authids.put(authDynaUserInfo.getAuthid(), new HashMap<String,HashSet<String>>());
			}			
			HashMap<String,HashSet<String>> pcolvalues = authids.get(authDynaUserInfo.getAuthid());
			
			if (null==pcolvalues.get(authDynaUserInfo.getPcolvalue())){
				pcolvalues.put(authDynaUserInfo.getPcolvalue(), new HashSet<String>());
			}
			pcolvalues.get(authDynaUserInfo.getPcolvalue()).add(authDynaUserInfo.getAuthkind());
		}
		if (log.isDebugEnabled()){
			printAuthDynaUserInfos();
		}
	}
	
	/**
	 * 控制器名对应的权限编号。
	 * h_auth_actions.actionname对应多个h_auth_actions.authid
	 */
	private HashMap<String, HashSet<Integer>> authActions = new HashMap<String, HashSet<Integer>>();
	private void clearAuthActions(){
		Set<Entry<String, HashSet<Integer>>> params = authActions.entrySet();
		for (Map.Entry<String, HashSet<Integer>> entry : params) {
			entry.getValue().clear();
		}
		authActions.clear();
	}
	
	/**
	 * 打印AuthActions
	 */
	@SuppressWarnings("unchecked")
	private void printAuthActions(){
		Set<Entry<String, HashSet<Integer>>> params = authActions.entrySet();
		for (Map.Entry<String, HashSet<Integer>> entry : params) {
			String actionname = entry.getKey();
			HashSet<Integer> authids = entry.getValue();
			log.info("h_auth_actions.actionname对应多个h_auth_actions.authid=====");
			log.info("actioinname="+actionname);
			log.info("对应authid：");
			log.info("   ---------------------------");
			for (Integer authid:authids){
				log.info("   authid="+authid);
			}
			log.info("====================================");
		}
	}
	
	/**
	 * 控制器类ScriptAction唯一编号id对应的树节点编号。
	 * h_auth_scriptactions.scid对应多个h_auth_actions.authid
	 */
	private HashMap<String, HashSet<Integer>> authScriptActions = new HashMap<String, HashSet<Integer>>();
	private void clearAuthScriptActions(){
		Set<Entry<String,HashSet<Integer>>> params = authScriptActions.entrySet();
		for (Map.Entry<String, HashSet<Integer>> entry : params) {
			entry.getValue().clear();
		}
		authScriptActions.clear();
	}
	
	/**
	 * 通过角色编号取该角色能允许访问的权限编号，通过权限编号能找到该权限上的所有许可。<br>
	 * HashMap<roleid, HashMap<authid,HashSet<authkind>>><br>
	 * 1.角色编号对应权限编号；<br>
	 * 2.权限编号对应权限各类。<br>
	 * h_auth_role_infos.roleid对应多个h_auth_role_infos.authid<br>
	 * h_auth_role_infos.authid对应多个h_auth_role_infos.authkind
	 */
	private HashMap<Integer, HashMap<Integer,HashSet<String>>> authRoleInfos = new HashMap<Integer, HashMap<Integer,HashSet<String>>>();
	private void clearAuthRoleInfos(){
		Set<Entry<Integer, HashMap<Integer,HashSet<String>>>> _maps = authRoleInfos.entrySet();
		for (Map.Entry<Integer, HashMap<Integer,HashSet<String>>> _map : _maps) {
			HashMap<Integer,HashSet<String>> authids = _map.getValue();
			Set<Entry<Integer, HashSet<String>>> params = authids.entrySet();
			for (Map.Entry<Integer, HashSet<String>> entry : params) {
				entry.getValue().clear();
			}
			_map.getValue().clear();
		}
		authRoleInfos.clear();
	}

	/**
	 * 打印AuthRoleInfos
	 */
	@SuppressWarnings("unchecked")
	private void printAuthRoleInfos(){
		log.info("==printAuthRoleInfos() 开始==========================");
		Set<Entry<Integer, HashMap<Integer,HashSet<String>>>> _maps = authRoleInfos.entrySet();
		for (Map.Entry<Integer, HashMap<Integer,HashSet<String>>> _map : _maps) {
			Integer roleid = _map.getKey();
			log.info("roleid="+roleid);
			HashMap<Integer,HashSet<String>> authids = _map.getValue();
			Set<Entry<Integer, HashSet<String>>> params = authids.entrySet();
			for (Map.Entry<Integer, HashSet<String>> entry : params) {
				Integer authid = entry.getKey();
				log.info("    authid="+authid);
				HashSet<String> authkinds = entry.getValue();
				for (String authkind:authkinds){
					log.info("        authkind="+authkind);
				}
				log.info("------------------------------------");
			}
		}
		log.info("==printAuthRoleInfos() 结束==========================");
	}
	
	/**
	 * 通过用户编号取该用户允许访问的权限编号，通过权限编号能找到该权限上的所有许可。<br>
	 * HashMap<userid, HashMap<authid,HashSet<authkind>>><br>
	 * 1.用户编号对应权限编号；<br>
	 * 2.权限编号对应权限各类。<br>
	 * h_auth_user_infos.userid对应多个h_auth_user_infos.authid<br>
	 * h_auth_user_infos.authid对应多个h_auth_user_infos.authkind
	 */
	private HashMap<Integer, HashMap<Integer,HashSet<String>>> authUserInfos = new HashMap<Integer, HashMap<Integer,HashSet<String>>>();
	private void clearAuthUserInfos(){
		Set<Entry<Integer, HashMap<Integer,HashSet<String>>>> _maps = authUserInfos.entrySet();
		for (Map.Entry<Integer, HashMap<Integer,HashSet<String>>> _map : _maps) {
			HashMap<Integer,HashSet<String>> authids = _map.getValue();
			Set<Entry<Integer, HashSet<String>>> params = authids.entrySet();
			for (Map.Entry<Integer, HashSet<String>> entry : params) {
				entry.getValue().clear();
			}
			_map.getValue().clear();
		}
		authUserInfos.clear();
	}
	
	/**
	 * 打印printAuthUserInfos
	 */
	@SuppressWarnings("unchecked")
	private void printAuthUserInfos(){
		log.info("==printAuthUserInfos() 开始==========================");
		Set<Entry<Integer, HashMap<Integer,HashSet<String>>>> _maps = authUserInfos.entrySet();
		for (Map.Entry<Integer, HashMap<Integer,HashSet<String>>> _map : _maps) {
			Integer Userid = _map.getKey();
			log.info("Userid="+Userid);
			HashMap<Integer,HashSet<String>> authids = _map.getValue();
			Set<Entry<Integer, HashSet<String>>> params = authids.entrySet();
			for (Map.Entry<Integer, HashSet<String>> entry : params) {
				Integer authid = entry.getKey();
				log.info("    authid="+authid);
				HashSet<String> authkinds = entry.getValue();
				for (String authkind:authkinds){
					log.info("        authkind="+authkind);
				}
				log.info("------------------------------------");
			}
		}
		log.info("==printAuthUserInfos() 结束==========================");
	}
	
	/**
	 * 通过角色编号取该角色能允许访问的扩展权限编号，通过扩展权限编号能找到该扩展权限上的所有许可。<br>
	 * HashMap<roleid, HashMap<authid,HashMap<pcolvalue,HashSet<authkind>>>><br>
	 * 1.角色编号对应动态权限编号；<br>
	 * 2.动态权限编号对应动态权限各类。<br>
	 * h_auth_dyna_role_infos.roleid对应多个h_auth_dyna_role_infos.authkey_pcolvalue<br>
	 * h_auth_dyna_role_infos.authkey_pcolvalue对应多个h_auth_dyna_role_infos.authkind<br>
	 * 
	 */
	private HashMap<Integer, HashMap<Integer,HashMap<String,HashSet<String>>>> authDynaRoleInfos = new HashMap<Integer, HashMap<Integer,HashMap<String,HashSet<String>>>>();
	private void clearAuthDynaRoleInfos(){
		Set<Entry<Integer,HashMap<Integer, HashMap<String,HashSet<String>>>>> dinfos = authDynaRoleInfos.entrySet();
		for(Map.Entry<Integer, HashMap<Integer,HashMap<String,HashSet<String>>>> _dinfo : dinfos){
			HashMap<Integer,HashMap<String,HashSet<String>>> _rmap = _dinfo.getValue();
			Set<Entry<Integer,HashMap<String, HashSet<String>>>> _maps = _rmap.entrySet();
			
			for (Map.Entry<Integer, HashMap<String,HashSet<String>>> _map : _maps) {
				HashMap<String,HashSet<String>> _authids = _map.getValue();
				Set<Entry<String, HashSet<String>>> params = _authids.entrySet();
				for (Map.Entry<String, HashSet<String>> entry : params) {
					entry.getValue().clear();
				}
				_authids.clear();
			}
			_rmap.clear();
		}
		authDynaRoleInfos.clear();
	}
	
	/**
	 * 打印AuthDynaRoleInfos
	 */
	@SuppressWarnings("unchecked")
	private void printAuthDynaRoleInfos(){
		log.info("==printAuthDynaRoleInfos() 开始==========================");
		authDynaRoleInfos.entrySet();
		Set<Entry<Integer, HashMap<Integer, HashMap<String, HashSet<String>>>>> _authDynaRoleInfos = authDynaRoleInfos.entrySet();
		for(Map.Entry<Integer, HashMap<Integer, HashMap<String, HashSet<String>>>> m :_authDynaRoleInfos){
			log.info("roleid=" + m.getKey());
			Set<Entry<Integer, HashMap<String, HashSet<String>>>> authmap = m.getValue().entrySet();
			for(Map.Entry<Integer, HashMap<String, HashSet<String>>> _authmap : authmap){
				log.info("authid="+_authmap.getKey());
				Set<Entry<String, HashSet<String>>> _subauthmap = _authmap.getValue().entrySet();
				for(Map.Entry<String, HashSet<String>> _authkind : _subauthmap){
					log.info("subauth="+_authkind.getKey());
					log.info("authkind="+_authkind.getValue());
				}
			}
		}
		log.info("==printAuthDynaRoleInfos() 结束==========================");
	}
	
	/**
	 * 通过用户编号取该用户能允许访问的扩展权限编号，通过扩展权限编号能找到该扩展权限上的所有许可。<br>
	 * HashMap<userid, HashMap<authid,HashMap<pcolvalue,HashSet<authkind>>>>
	 * 1.用户编号对应动态权限编号；<br>
	 * 2.动态权限编号对应动态权限各类。<br>
	 * h_auth_dyna_user_infos.Userid对应多个h_auth_dyna_user_infos.authkey_pcolvalue<br>
	 * h_auth_dyna_user_infos.authkey_pcolvalue对应多个h_auth_dyna_user_infos.authkind<br>
	 */
	private HashMap<Integer, HashMap<Integer,HashMap<String,HashSet<String>>>> authDynaUserInfos = new HashMap<Integer, HashMap<Integer,HashMap<String,HashSet<String>>>>();
	private void clearAuthDynaUserInfos(){
		Set<Entry<Integer,HashMap<Integer, HashMap<String,HashSet<String>>>>> dinfos = authDynaUserInfos.entrySet();
		for(Map.Entry<Integer, HashMap<Integer,HashMap<String,HashSet<String>>>> _dinfo : dinfos){
			HashMap<Integer,HashMap<String,HashSet<String>>> _rmap = _dinfo.getValue();
			Set<Entry<Integer,HashMap<String, HashSet<String>>>> _maps = _rmap.entrySet();
			for (Map.Entry<Integer, HashMap<String,HashSet<String>>> _map : _maps) {
				HashMap<String,HashSet<String>> _authids = _map.getValue();
				Set<Entry<String, HashSet<String>>> params = _authids.entrySet();
				for (Map.Entry<String, HashSet<String>> entry : params) {
					entry.getValue().clear();
				}
				_authids.clear();
			}
			_rmap.clear();
		}
		authDynaUserInfos.clear();
	}
	
	//HashMap<authid,HashMap<scriptid,HashSet<authkindid>>>
	private HashMap<Integer,HashMap<String, HashSet<Integer>>> authScriptidKind = new HashMap<Integer,HashMap<String, HashSet<Integer>>>();
	private void clearAuthScriptidKind(){
		Set<Entry<Integer,HashMap<String, HashSet<Integer>>>> _authScriptid = authScriptidKind.entrySet();
		for(Map.Entry<Integer,HashMap<String, HashSet<Integer>>> _mauthScriptid : _authScriptid){
			HashMap<String, HashSet<Integer>> _mvauthScriptid = _mauthScriptid.getValue();
			Set<Entry<String, HashSet<Integer>>> _scriptidkind = _mvauthScriptid.entrySet();
			for (Map.Entry<String, HashSet<Integer>> _mscriptidkind : _scriptidkind) {
				 _mscriptidkind.getValue().clear();
			}
			_mvauthScriptid.clear();
		}
		authScriptidKind.clear();
	}
	
	private void printAuthScriptKindAction(){
		
	}
	 
	//HashMap<authid,HashMap<actionname,HashSet<authkindid>>>
	private HashMap<Integer,HashMap<String, HashSet<Integer>>> authActionKind = new HashMap<Integer,HashMap<String, HashSet<Integer>>>();
	private void clearAuthActionKind(){
		Set<Entry<Integer,HashMap<String, HashSet<Integer>>>> _authActionKind = authActionKind.entrySet();
		for(Map.Entry<Integer,HashMap<String, HashSet<Integer>>> _mauthAction : _authActionKind){
			HashMap<String, HashSet<Integer>> _mvauthAction = _mauthAction.getValue();
			Set<Entry<String, HashSet<Integer>>> _actionkind = _mvauthAction.entrySet();
			for (Map.Entry<String, HashSet<Integer>> _mactionkind : _actionkind) {
				_mactionkind.getValue().clear();
			}
			_mvauthAction.clear();
		}
		authActionKind.clear();
	}
	
	private void printAuthActionKindAction(){
		
	}
	
	/**
	 * 打印AuthDynaRoleInfos
	 */
	@SuppressWarnings("unchecked")
	private void printAuthDynaUserInfos(){
		log.info("==printAuthDynaUserInfos() 开始==========================");

		log.info("==printAuthDynaUserInfos() 结束==========================");
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList findAll(String tableName,String condition,Class beanClass,String col){
		if (null == col || "".equals(col)) col = "*";
		StringBuffer sb = new StringBuffer();
		sb.append("select ").append(col).append(" from ").append(tableName).append(" ").append(condition);
		//log.info(sb.toString());
		BeanListHandler h = new BeanListHandler(beanClass);
		QueryRunner run = new QueryRunner(dbUtil.dataSource(dataSourceName));
		ArrayList list = null;
		try {
			list = (ArrayList)run.query(sb.toString(), h);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList findAll(String tableName,String condition,Class beanClass){
		return findAll(tableName,condition,beanClass,null);
	}
	
	private String dataSourceName = "";
	
	private static final String TABLE_H_AUTH_ACTIONS = "h_auth_actions";//权限树节点对应的action控制器表
	private static final String TABLE_H_AUTH_SCRIPTACTIONS = "h_auth_scriptactions";//权限树节点对应的ScriptAction控制器scid表
	private static final String TABLE_H_AUTH_ROLE_INFOS = "h_auth_role_infos";//存储角色权限分配情况表
	private static final String TABLE_H_AUTH_USER_INFOS = "h_auth_user_infos";//存储用户权限分配情况表
	private static final String TABLE_H_AUTH_DYNA_ROLE_INFOS = "h_auth_dyna_role_infos";//存储角色动态权限分配情况表
	private static final String TABLE_H_AUTH_DYNA_USER_INFOS = "h_auth_dyna_user_infos";//存储用户动态权限分配情况表
	
	public static final Log log = LogFactory.getLog(TAuthCache.class);
	private DBUtil dbUtil = DBUtil.getInstance();
}
